package Problem4;
import java.util.Arrays;
public class BinaryLookup {
    public static void main(String[] args) {
        int[] arr = {7,7,7,8,8,8,10};
        int target = 9;
        int[] a = lookupRightAndLeft(arr,target);
        System.out.println(Arrays.toString(a));
    }
    public static int lookupLeft(int[] arr, int target){
        int left = 0;
        int right = arr.length-1;
        int isLeft = - 1;
        while (left <= right){
            int index = (left+right)/2;
            if ( arr[index] == target){
                isLeft = index;
                right = index - 1;
            } else if (arr[index] > target) {
                right = index - 1;
            }else {
                left = index + 1;
            }
        }
        return isLeft;
    }
    public static int lookupRight(int[] arr, int target){
        int left = 0;
        int right = arr.length-1;
        int isRight = - 1;
        while (left <= right){
            int index = (left+right)/2;
            if ( arr[index] == target){
                isRight = index;
                left = index + 1;
            } else if (arr[index] > target) {
                right = index - 1;
            }else {
                left = index + 1;
            }
        }
        return isRight;
    }
    public static int[] lookupRightAndLeft(int[] arr,int target){
        int[] returnArr = new int[2];
        int isLeft = lookupLeft(arr,target);
        int isRight = lookupRight(arr,target);
        returnArr[0] = isLeft;
        returnArr[1] = isRight;
        return returnArr;
    }
}
